//622. 设计循环队列


#include <stdlib.h>
#include <stdbool.h>

typedef struct {
	int front;
	int rear;
	int k;
	int* a;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
	MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
	obj->a = (int*)malloc(sizeof(int) * (k + 1));//数组开空间的时候多开一个位置,区别空和满
	obj->front = obj->rear = 0;
	obj->k = k;

	return obj;
}

//判空
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
	return obj->front == obj->rear;
}

//判满
bool myCircularQueueIsFull(MyCircularQueue* obj) {
	return (obj->rear + 1) % (obj->k + 1) == obj->front;
}

//入队
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
	if (myCircularQueueIsFull(obj))
		return false;

	obj->a[obj->rear] = value;
	obj->rear++;
	obj->rear %= (obj->k + 1);

	return true;
}

//出队
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
	if (myCircularQueueIsEmpty(obj))
		return false;

	obj->front++;
	obj->front %= (obj->k + 1);

	return true;
}

//取队头
int myCircularQueueFront(MyCircularQueue* obj) {
	if (myCircularQueueIsEmpty(obj))
		return -1;

	return obj->a[obj->front];
}

//取队尾
int myCircularQueueRear(MyCircularQueue* obj) {
	if (myCircularQueueIsEmpty(obj))
		return -1;

	return obj->a[(obj->rear + obj->k) % (obj->k + 1)];//不化简(rear-1 + k+1) % (k+1),
}													//这是特殊情况，rear=0,需要找尾rear-1就出错了

//销毁队
void myCircularQueueFree(MyCircularQueue* obj) {
	free(obj->a);
	free(obj);
}